$(function(){

	var curEditDictId = null;

	function DictCreateModal(modalEle) {
		var self = this;
		self.$ele = modalEle;
		$('.save-btn',self.$ele).on('click',function(){
			self._onSave();
		});	
		$('.cancel-btn',self.$ele).on('click',function() {
			self._onCancel();
		});	
		self.tagBox = new TagBox($('.tag-box',self.$ele));
		self._initModal();
		self._initValidate();
	}

	DictCreateModal.prototype = {
		constructor: DictCreateModal,
		$ele: null,		
		$permissionBox: null,
		$selectBox: null,
		$validater: null,
		tagBox: null,
		_initModal: function () {
			$(this.$ele).modal({
				backdrop: 'static',
				keyboard: false,	
				show: false
			});
		},
		_initValidate: function() {
			this.validater = $('#dict-create-form').validate({
				rules: {},
				messages:
						{},
				showErrors: function(errorMap, errorList){
								if(errorList.length > 0) {
									var error = errorList[0];
									$(error.element).parent().addClass('has-error');
									$('#dict-create-msg').show().find('p').text(error.message);
								}
							},
				onkeyup: function(ele) {
							$('#dict-create-msg').hide();
							$(ele).parent().removeClass('has-error');
						 },
				success: function(label,ele) {
							 console.log(lable);
							 console.log(ele);
						 },
				submitHandler: function(){
							$('#dict-create-box .overlay').show();
							saveDict();
						   }
				
			});
	   },
		getCheckedIdArr: function(){
			return this.tagBox.getCheckedTagIdArray();
		},
		_onCancel: function() {
			$(this.$ele).modal('hide');
		},
		_onSave: function() {
			$('form',this.$ele).submit();
		},
		_reset: function () {
			this.tagBox.reset();
			$('#dict-create-modal input:text').each(function(){
				$(this).val('');
			});
			$('#dict-create-msg').hide();
			$('.has-error').removeClass('has-error');
			$('#dict-create-box .overlay').hide();
		},
		close: function () {
			$(this.$ele).modal('hide');
		},
		open: function (dictInfo) {
			this._reset();
			//this.tagBox.setChecks(roleIdArr);
			if(dictInfo) {
				$('.dict-create-title',this.$ele).html('编辑字典');
				for(var key in dictInfo) {
					$('input[name="'+key+'"]',this.$ele).val(dictInfo[key]);
				}
			} else {
				$('.dict-create-title',this.$ele).html('新建字典');
				$('#parentItemId').val("") ;
				$('#isValid').val("1");
				$('#itemSort').val("0");
			}
		//	if(roleIdArr) {
		//	}
			$(this.$ele).modal('show');
		}
	};

	var dictCreater = new DictCreateModal($('#dict-create-modal'));

	$('#dict-create-btn').on('click',function(){
		curEditDictId = null;
		dictCreater.open();
	});

	$('.edit-btn').on('click',function(){
		var $tr = $(this).parents('tr');
		curEditDictId = $tr.attr('data-uid');
		cAjax({
			url: '/crm/dataDict/editDataDict',
			type: 'get',
			data: {
				uid: curEditDictId 
			},
			success: function(obj) {
					 console.log(obj);
				/*	var roleIds = [];
					var roleList = obj.data['roleList'];
					for(var index in roleList) {
						roleIds.push(roleList[index]['id']);
					}*/
					var dictInfo = obj.data;
					//alert(dictInfo.parentItemId);
					$("#parentItemId").val(dictInfo.parentItemId);
					$("#isValid").val(dictInfo.isValid);
					//alert($("#isValid").val());
					dictCreater.open(dictInfo);
			},
			error: function(obj) {
						 console.log(obj);
		    }
		});
	});

	function saveDict() {
		var data = {},
			url = '';
		//data['dictName'] = $.trim($('#role-name').val());
		$('#dict-create-modal input:text').each(function(){
			var $this = $(this);
			data[$this.attr('name')] = $.trim($this.val());
		});
		$('#dict-create-modal select').each(function(){
			var $this = $(this);
			//alert($this.val());
			//alert($this.attr('name'));
			data[$this.attr('name')] = $.trim($this.val());
		});
		//data['parentItemId'] =  $("#parentItemId").find("option:selected").text();
		//alert(data['parentItemId']);
		if(curEditDictId == null) {
			url = '/crm/dataDict/addDataDict';
		} else {
			data['id'] = curEditDictId;
			url = '/crm/dataDict/editDataDict';
		}
		console.log(data);
		dictCreater.close();

		cAjax({
			url: url,
			data: data,
			success: function(obj) {
				console.log(obj);
				greeting({
					msg: "字典操作成功!",
					confirm: function(){
						window.location.reload();
					}

				});
			},
			error: function(obj){
				console.log(obj);
		   }
		});
	}

	$('.dict-del-btn').on('click',function(){
		var $this = $(this);
		actionConfirm({
			msg: "确定要删除这条字典吗？",
			confirm: function() {
				cAjax({
					url:'/crm/dataDict/deleteDataDict',
					type:'post',
					data:{uid: $this.parents('tr').attr('data-uid')},
					success:function(obj){
						console.log(obj);
						$this.parents('tr').addClass('deleted');
						$this.parent().html('<span class="label label-warning">已删除</span>');
					},
					error: function(obj) {
						
						console.log(obj);
						greeting({
							msg: obj.msg
						})
				   }
				});
			}
		});
	});
});
